package com.yxinsur.imc.web.controller.business;

import java.util.ArrayList;
import java.util.List;

import com.yxinsur.imc.business.domain.ImcSupplierPersistency;
import com.yxinsur.imc.business.form.ImcSuppierPersistencyForm;
import com.yxinsur.imc.business.form.SupplierListForm;
import com.yxinsur.imc.business.service.IImcOrderRiskService;
import com.yxinsur.imc.business.service.IImcSupplierPersistencyService;
import com.yxinsur.imc.business.vo.DenominatorOrderVo;
import com.yxinsur.imc.business.vo.ImcSupplierPersistencyVo;
import com.yxinsur.imc.business.vo.SupplierVo;
import com.yxinsur.imc.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yxinsur.imc.common.annotation.Log;
import com.yxinsur.imc.common.core.controller.BaseController;
import com.yxinsur.imc.common.core.domain.AjaxResult;
import com.yxinsur.imc.common.enums.BusinessType;
import com.yxinsur.imc.common.core.page.TableDataInfo;

/**
 * 供应商继续率主Controller
 *
 * @author imc
 * @date 2021-09-23
 */
@Api(value="保险公司继续率主表配置",tags={"保险公司继续率主表配置"})
@RestController
@RequestMapping("/business/persistency")
public class ImcSupplierPersistencyController extends BaseController {
    @Autowired
    private IImcSupplierPersistencyService imcSupplierPersistencyService;

    /**
     * 查询供应商继续率主列表
     */
    @ApiOperation("查询保险公司继续率主表配置列表")
    @GetMapping("/list")
    public TableDataInfo list(ImcSuppierPersistencyForm imcSuppierPersistencyForm) {
        startPage();
        List<ImcSupplierPersistencyVo> list = imcSupplierPersistencyService.selectImcSupplierPersistencyList(imcSuppierPersistencyForm);
        return getDataTable(list);
    }


    /**
     * 获取供应商继续率主详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:persistency:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(imcSupplierPersistencyService.selectImcSupplierPersistencyById(id));
    }

    /**
     * 新增供应商继续率主
     */
    @PreAuthorize("@ss.hasPermi('system:persistency:add')")
    @Log(title = "供应商继续率主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ImcSupplierPersistency imcSupplierPersistency) {
        return toAjax(imcSupplierPersistencyService.insertImcSupplierPersistency(imcSupplierPersistency));
    }

    /**
     * 修改供应商继续率主
     */
    @PreAuthorize("@ss.hasPermi('system:persistency:edit')")
    @Log(title = "供应商继续率主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ImcSupplierPersistency imcSupplierPersistency) {
        return toAjax(imcSupplierPersistencyService.updateImcSupplierPersistency(imcSupplierPersistency));
    }

    /**
     * 删除供应商继续率主
     */
    @ApiOperation("删除保险公司继续率主表配置")
    @Log(title = "删除保险公司继续率主表配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(imcSupplierPersistencyService.deleteImcSupplierPersistencyByIds(ids));
    }

    /**
     * 保险公司继续率计算
     */
    @ApiOperation("保险公司继续率计算")
    @Log(title = "保险公司继续率计算", businessType = BusinessType.INSERT)
    @PostMapping("/calculate/{perConfigId}/{calculateDate}")
    public AjaxResult calculate(@PathVariable Long perConfigId,@PathVariable String calculateDate){
        //计算校验同一家保险公司在一个结算月内只能有一条数据
        imcSupplierPersistencyService.checkSupplierCalculate(perConfigId,calculateDate);
        return toAjax(imcSupplierPersistencyService.calculate(perConfigId,calculateDate));
    }


    /**
     * 查询主表的分母保单详情信息
     */
    @ApiOperation("查询主表的分母保单详情信息")
    @GetMapping("/orderList/{supplierPersistencyId}")
    public TableDataInfo orderList(@PathVariable Long supplierPersistencyId) {

        ArrayList<String> orderRiskIdList= imcSupplierPersistencyService.selectDenominatorOrderParam(supplierPersistencyId);
        startPage();
        return getDataTable(imcSupplierPersistencyService.selectDenominatorOrderList(orderRiskIdList));
    }


    /**
     * 查询主表的分母保单详情信息
     */
    @ApiOperation("导出主表的分母保单详情信息")
    @Log(title = "导出主表的分母保单详情信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export/{supplierPersistencyId}")
    public AjaxResult export(@PathVariable Long supplierPersistencyId) {
        ArrayList<String> orgIdList = imcSupplierPersistencyService.selectDenominatorOrderParam(supplierPersistencyId);
        List<DenominatorOrderVo> list = imcSupplierPersistencyService.selectDenominatorOrderList(orgIdList);
        ExcelUtil<DenominatorOrderVo> util = new ExcelUtil<DenominatorOrderVo>(DenominatorOrderVo.class);
        return util.exportExcel(list, "保单详情信息");
    }
}
